<template>
  <div class="login-wrapper">
    <div class="model">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
        <div class="title">云顶</div>
        <el-form-item prop="userName">
          <el-input
            placeholder="请输入账号"
            v-model="ruleForm.userName"
            type="text"
            clearable
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input
            placeholder="请输入密码"
            v-model="ruleForm.passWord"
            type="password"
            clearable
            :prefix-icon="View"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-button"
            type="primary"
            @click="handleLogin(ruleFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { User, View } from "@element-plus/icons-vue";
import { reactive, ref, getCurrentInstance  } from "vue";
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'

export default {
  name: "Login",
  setup() {
    // Vue3 调用全局方法或变量的方式
    const context = getCurrentInstance()?.appContext.config.globalProperties;

    const store = useStore()
    const router = useRouter();

    const ruleForm = reactive({
      userName: "",
      passWord: "",
    });
    const rules = reactive({
      userName: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      passWord: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
      ],
    });
    const ruleFormRef = ref();
    const handleLogin = (ruleFormRef) => {
      if (!ruleFormRef) return;
      ruleFormRef.validate(async (valid) => {
        if (valid) {
          const res = await context.$api.login(ruleForm);
          ElMessage.success("操作成功")
          store.commit("savaUserInfo", res);
          router.push('/welcome')
        }
      });
    };
    return {
      User,
      View,
      ruleForm,
      handleLogin,
      rules,
      ruleFormRef,
    };
  },
};
</script>

<style lang="scss">
.login-wrapper {
  // background-image: url(../assets/images/back.jpeg);
  // background-repeat: no-repeat;
  // background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  .model {
    width: 500px;
    height: 400px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 10px 19px 30px 3px #c7c9cb;
    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .login-button {
      width: 100vh;
      height: 40px;
    }
  }
}
</style>